{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from DrissionPage import ChromiumPage\n",
    "import threading\n",
    "\n",
    "tab = ChromiumPage()\n",
    "# tab.set.window.hide()\n",
    "tab1 = tab.new_tab('https://vmnppmls6.com/plugin.php?id=k_misign:sign')\n",
    "tab2 = tab.new_tab('https://vmnppmls6.com/plugin.php?id=k_misign:sign')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def split_image(image_data):\n",
    "    from PIL import Image\n",
    "    import io\n",
    "    import ddddocr\n",
    "    # 打开原始图片\n",
    "    # image = Image.open(input_path)\n",
    "    image = Image.open(io.BytesIO(image_data))\n",
    "\n",
    "    # 获取图片的宽度和高度\n",
    "    width, height = 250, 450  #image.size\n",
    "\n",
    "    # 设置每张小图的大小为100x100像素\n",
    "    small_width = 250\n",
    "    small_height = 150\n",
    "\n",
    "    # 计算需要切割成多少张小图\n",
    "    num_rows = height // small_height + (1 if height %\n",
    "                                         small_height != 0 else 0)\n",
    "    num_cols = width // small_width + (1 if width % small_width != 0 else 0)\n",
    "    img = []\n",
    "    for row in range(num_rows):\n",
    "        for col in range(num_cols):\n",
    "            left = col * small_width\n",
    "            upper = row * small_height\n",
    "\n",
    "            right = min((col + 1) * small_width, width)\n",
    "            lower = min((row + 1) * small_height, height)\n",
    "\n",
    "            box = (left, upper, right, lower)\n",
    "            # 裁剪并保存小图\n",
    "            cropped_image = image.crop(box)\n",
    "            img.append(cropped_image)\n",
    "\n",
    "    img_list = []\n",
    "    for i in img:\n",
    "        a = io.BytesIO()\n",
    "        i.save(a, 'png')\n",
    "        img_list.append(a)\n",
    "\n",
    "    slide = ddddocr.DdddOcr(det=False, ocr=False, show_ad=False)\n",
    "\n",
    "    # # res = slide.slide_comparison(img_io1.getvalue(),img_io2.getvalue())\n",
    "    res = slide.slide_comparison(img_list[0].getvalue(),\n",
    "                                 img_list[2].getvalue())\n",
    "\n",
    "    return res['target']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def Verification_code(tab,wb):\n",
    "\n",
    "    # 验证码识别\n",
    "    res = tab.listen.wait()  # 等待并获取一个数据包\n",
    "    img = res.response.body\n",
    "    url_r = split_image(img)[0]\n",
    "    slider = tab('tag:div@@class=slide_block')\n",
    "    slider.drag(url_r-5, duration=1)\n",
    "    tab.wait(1)\n",
    "    if wb == '登录':\n",
    "        if '成功' in tab('tag:div@@id=tncode').text:\n",
    "            tab('tag:button@@id=captcha_submit').click('js')\n",
    "        else:\n",
    "            tab('tag:div@@class=tncode_refresh',timeout=1).click()\n",
    "            Verification_code(tab,wb)\n",
    "    else:\n",
    "        if '成功' in tab('tag:div@@id=tncode').text:\n",
    "            tab('tag:button@@id=submit-btn').click('js')\n",
    "        else:\n",
    "            tab('tag:div@@class=tncode_refresh',timeout=1).click()\n",
    "            Verification_code(tab,wb)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def signin(tab):\n",
    "    tab.get('https://vmnppmls6.com/plugin.php?id=k_misign:sign')\n",
    "    try:\n",
    "        tab('tag:a@@class=btn J_chkitot').click()\n",
    "        tab.listen.start('/captcha/tncode.php')  # 开始监听，指定获取包含该文本的数据包\n",
    "        tab('tag:div@@id=tncode').click()\n",
    "        Verification_code(tab,'验证码')\n",
    "        tab.listen.stop()  # 停止监听\n",
    "        return print('刚刚签到')\n",
    "    except:\n",
    "        return print('已经签到')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "def Login_Account(tab, name,AccountNumber):\n",
    "    try:\n",
    "        tab('点').click()\n",
    "    except:\n",
    "        pass\n",
    "    try:\n",
    "        tab('一天内不再提醒').click()\n",
    "    except:\n",
    "        pass\n",
    "    # 打开登录页面\n",
    "    try:\n",
    "        if name in tab('t:div@@class=user').text:\n",
    "            print('已经登录')\n",
    "        else:\n",
    "            print('帐号不对')\n",
    "            tab('tag:i@@text()=').hover()\n",
    "            tab('tag:a@@text()=退出').click()\n",
    "            tab.wait(1)\n",
    "\n",
    "    except:\n",
    "        print('登录页面打开失败')\n",
    "\n",
    "    tab('tag:a@@class=btn J_chkitot').click()\n",
    "    # 登录账号\n",
    "    if tab1.s_ele('帐号'):\n",
    "        print('登录账号')\n",
    "        tab('@name=username').input(AccountNumber['username'], by_js=True)\n",
    "        tab('@name=password').input(AccountNumber['password'], by_js=True)\n",
    "        tab.listen.start('/captcha/tncode.php')  # 开始监听，指定获取包含该文本的数据包\n",
    "        tab('tag:div@@id=tncode').click()\n",
    "        Verification_code(tab,'登录')\n",
    "        tab.listen.stop()  # 停止监听\n",
    "        tab.wait(1)\n",
    "    signin(tab)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "liang008\n"
     ]
    }
   ],
   "source": [
    "print(tab1('t:div@@class=user').text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "帐号不对\n",
      "已经登录\n",
      "登录账号\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Exception in thread Thread-12 (Login_Account):\n",
      "Traceback (most recent call last):\n",
      "  File \"C:\\Program Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.11_3.11.2544.0_x64__qbz5n2kfra8p0\\Lib\\threading.py\", line 1045, in _bootstrap_inner\n",
      "    self.run()\n",
      "  File \"C:\\Users\\00\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python311\\site-packages\\ipykernel\\ipkernel.py\", line 766, in run_closure\n",
      "    _threading_Thread_run(self)\n",
      "  File \"C:\\Program Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.11_3.11.2544.0_x64__qbz5n2kfra8p0\\Lib\\threading.py\", line 982, in run\n",
      "    self._target(*self._args, **self._kwargs)\n",
      "  File \"C:\\Users\\00\\AppData\\Local\\Temp\\ipykernel_19272\\520600571.py\", line 23, in Login_Account\n",
      "  File \"C:\\Users\\00\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python311\\site-packages\\DrissionPage\\_elements\\none_element.py\", line 38, in __getattr__\n",
      "    raise ElementNotFoundError(None, self.method, self.args)\n",
      "DrissionPage.errors.ElementNotFoundError: \n",
      "没有找到元素。\n",
      "method: ele()\n",
      "args: {'locator': 'tag:a@@class=btn J_chkitot', 'index': 1, 'timeout': 10}\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "刚刚签到\n"
     ]
    }
   ],
   "source": [
    "liang008 = {'username': '17677051263@163.com', 'password': 'Lz19990311.'}\n",
    "liang007 = {'username': '3233537658@qq.com', 'password': 'Lz19990311.'}\n",
    "# 创建线程对象\n",
    "thread1 = threading.Thread(target=Login_Account, args=(tab1,'liang007',liang007))\n",
    "thread2 = threading.Thread(target=Login_Account, args=(tab2,'liang008' ,liang008))\n",
    "\n",
    "# 启动线程\n",
    "thread1.start()\n",
    "thread2.start()\n",
    "\n",
    "# 等待线程结束\n",
    "thread1.join()\n",
    "thread2.join()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
